package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.MarketGoodsBo;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.service.MarketGoodsService;
import com.cskaoyan.market.service.MarketGoodsServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

// 1.接受请求参数 抓包  2.处理业务逻辑  3.返回响应 抓公网的包，和公网上返回的响应一样
@WebServlet("/admin/goods/*")
public class AdminGoodsController extends HttpServlet {
    private MarketGoodsService goodsService = new MarketGoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/goods/", "");
        if ("list".equals(op)) {
            list(req, resp);
        }
        else if ("catAndBrand".equals(op)) {
            catAndBrand(req, resp);
        }
        else if ("detail".equals(op)) {
            detail(req, resp);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获取请求参数
        String pageParam = req.getParameter("page");
        String limitParam = req.getParameter("limit");
        String goodsSn = req.getParameter("goodsSn");
        String name = req.getParameter("name");
        String goodsId = req.getParameter("goodsId");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        Integer page = null;
        Integer limit = null;
        //如果输入参数不是数字
        try {
            page = Integer.parseInt(pageParam);
            limit = Integer.parseInt(limitParam);
        } catch (Exception e) {
            //输出参数错误
            Object o = ResponseUtil.badArgument();
            //将json文本转换为字符串
            String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
            resp.getWriter().println(s);
        }
        //处理业务逻辑
        List<MarketGoods> marketGoods = goodsService.list(page, limit, goodsSn, name, sort, order, goodsId);
        //返回响应
        Object o = ResponseUtil.okList(marketGoods);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));
    }


    private void detail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        if(id==null){
            resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.badArgument()));
            return;
        }
        Integer goodsId = Integer.parseInt(id);
        Map<String, Object> data = goodsService.detail(goodsId);
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));
    }

    private void catAndBrand(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, Object> data = goodsService.catAndBrand();
        Object ok = ResponseUtil.ok(data);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ok));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/goods/", "");
        if ("update".equals(op)) {
            update(req, resp);
        }
        else if ("create".equals(op)) {
            create(req, resp);
        }
        else if ("delete".equals(op)) {
            delete(req,resp);
        }
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        BufferedReader requestBody = req.getReader();
        MarketGoods goods = JacksonUtil.getObjectMapper().readValue(requestBody, MarketGoods.class);
        goodsService.delete(goods);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketGoodsBo marketGoodsBo = objectMapper.readValue(requestBody, MarketGoodsBo.class);
        goodsService.create(marketGoodsBo.getGoods(), marketGoodsBo.getProducts(), marketGoodsBo.getSpecifications(), marketGoodsBo.getAttributes());
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        MarketGoodsBo marketGoodsBo = JacksonUtil.getObjectMapper().readValue(requestBody, MarketGoodsBo.class);
        String name = marketGoodsBo.getGoods().getName();
        if (StringUtils.isEmpty(name)) {
            Object o = ResponseUtil.badArgument();
            resp.getWriter().println(o);
            return;
        }
        goodsService.update(marketGoodsBo.getGoods(), marketGoodsBo.getProducts(), marketGoodsBo.getSpecifications(), marketGoodsBo.getAttributes());
        System.out.println();
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(ResponseUtil.ok()));
    }

}

